package com.googlecode.cswish.struts.spring;

import java.lang.reflect.Method;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Service;

import com.googlecode.cswish.Config;
import com.googlecode.cswish.util.FrameConstant;
import com.opensymphony.xwork2.Action;

/**	 
 * @author Jerry.feng Date: 19/09/2008
 */
@Service
public class DummyBean implements FactoryBean {
	private static final Logger logger = Logger.getLogger(DummyBean.class);
	
	@Resource
	private DFactory dfactory;
	
	@Resource
	private Config config;
	
	public void execute() {
		logger.debug("DummyBean.execute");
	}

	@Override
	public Object getObject() throws Exception {
		if (config == null) {
			return null;
		}
		
		try {
			Class<?>[] params = null;
			Method execute = DummyBean.class.getMethod("execute", params);
			Class<?> clazz = dfactory.convert2Class(DummyBean.class, execute, getActionName(),
					config.getBaseAction(), "execute", true, Action.SUCCESS);
			return clazz.newInstance();
		} catch (Exception ex) {
			logger.error("Fail to create Dummy Action", ex);
		}
		return null;
	}
	
	// beanIntrospector is null when DummyBean is created at the first time
	private String getActionName() {
		return FrameConstant.PACKAGE_PREFIX + "dummy.Execute" + FrameConstant.ACTION_PACKAGE_SUFFIX;
	}

	@Override
	public Class getObjectType() {
		if (config == null) {
			return null;
		}
		return Object.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}
}